package com.tongchengsoft.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.tongchengsoft.bean.BeanUsers;
import com.tongchengsoft.server.IUsersService;
import com.tongchengsoft.tools.StringTools;

@Controller
@RequestMapping("user")
public class UsersController {

	@Autowired
	private IUsersService userService;
	
	@RequestMapping(value = "/login")
	public String Login(HttpServletRequest request, HttpServletResponse response){
		
		return null;
	}
	
	@RequestMapping(value = "/loginOut")
	public String LoginOut(HttpServletRequest request, HttpServletResponse response){
		
		return null;
	}
	
	@RequestMapping(value = "/addJump")
	public String AddJump(HttpServletRequest request, HttpServletResponse response){
		
		return "users/add";
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView Add(HttpServletRequest request, HttpServletResponse response){
		
		String yhBh = request.getParameter("yhBh");
		String yhXm = request.getParameter("yhXm");
		String yhMm = request.getParameter("yhMm");
		String yhBm = request.getParameter("yhBm");
		ModelMap mm = new ModelMap();
		
		try {
			
			if(StringTools.isEmpty(yhBm) || !StringTools.isNumber(yhBm)){
				
				mm.put("msg", "添加失败！");
				return new ModelAndView("redirect:findAll", mm);
			}
			
			BeanUsers user = new BeanUsers(yhBh, yhXm, yhMm, Integer.valueOf(yhBm));
			if(userService.add(user))
				
				mm.put("msg", "添加成功！");
			else
				
				mm.put("msg", "添加失败！");
				
		} catch (Exception e) {

			e.printStackTrace();
		}
		return new ModelAndView("redirect:findAll", mm);
	}
	
	@RequestMapping(value = "/delete")
	public ModelAndView Delete(HttpServletRequest request, HttpServletResponse response){
		
		String lsId = request.getParameter("lsId");
		ModelMap mm = new ModelMap();
		
		try {
			
			if(StringTools.isEmpty(lsId) || !StringTools.isNumber(lsId)){
				
				mm.put("msg", "删除失败！");
				return new ModelAndView("findAll", mm);
			}
			BeanUsers user = new BeanUsers(Integer.valueOf(lsId));
			if(userService.delete(user))
				
				mm.put("msg", "删除成功！");
			else
				
				mm.put("msg", "删除失败！");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("redirect:findAll", mm);
	}
	
	@RequestMapping(value = "/updateJump")
	public ModelAndView UpdateJump(HttpServletRequest request, HttpServletResponse response){
		
		String lsId = request.getParameter("lsId");
		ModelMap mm = new ModelMap();
		
		try {
			
			if(StringTools.isEmpty(lsId) || !StringTools.isNumber(lsId)){
				
				mm.put("msg", "获取部门信息失败！");
				return new ModelAndView("redirect:findAll", mm);
			}
			BeanUsers user =  userService.findByLsId(Integer.valueOf(lsId));
			request.setAttribute("user", user);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("users/update");
	}
	
	@RequestMapping(value = "/update")
	public ModelAndView Update(HttpServletRequest request, HttpServletResponse response){
		
		String lsId = request.getParameter("lsId");
		String yhBh = request.getParameter("yhBh");
		String yhXm = request.getParameter("yhXm");
		String yhMm = request.getParameter("yhMm");
		String yhBm = request.getParameter("yhBm");
		String isEnable = request.getParameter("isEnable");
		ModelMap mm = new ModelMap();
		
		try {
			
			if(StringTools.isEmpty(lsId) || !StringTools.isNumber(lsId) || StringTools.isEmpty(yhBm) || !StringTools.isNumber(yhBm)){
				
				mm.put("msg", "修改失败！");
				return new ModelAndView("redirect:findAll", mm);
			}
			BeanUsers user = new BeanUsers(Integer.valueOf(lsId), yhBh, yhXm, yhMm, Integer.valueOf(yhBm), isEnable);
			if(userService.update(user))
				
				mm.put("msg", "修改成功！");
			else
				
				mm.put("msg", "修改失败！");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("redirect:findAll", mm);
	}
	
	@RequestMapping(value = "/findAll")
	public ModelAndView FindAll(HttpServletRequest request, HttpServletResponse response){
		
		ModelMap mm = new ModelMap();
		
		try {
			mm.put("msg", request.getAttribute("msg"));
			List<BeanUsers> uList = userService.find(null);
			request.setAttribute("uList", uList);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("users/list", mm);
	}
}
